package com.cskaoyan.wordmemorize.converter;


import com.cskaoyan.wordmemorize.dao.entity.VocCategoryDO;
import com.cskaoyan.wordmemorize.dto.admin.DeleteBookCategoryOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocCategoryDTO;
import com.cskaoyan.wordmemorize.request.VocCategoryCommand;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy;
import org.springframework.util.StringUtils;

import java.util.List;

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface VocCategoryConverter {

    @Mapping(source = "id", target = "id", qualifiedByName = "stringToLong")
    VocCategoryDO vocCategoryCommand2Entity(VocCategoryCommand command);
    
    /**
     * 将字符串ID转换为Long类型，处理空字符串的情况
     * @param id 字符串ID
     * @return Long类型ID，如果字符串为空则返回null
     */
    @Named("stringToLong")
    default Long stringToLong(String id) {
        if (!StringUtils.hasLength(id) || id.trim().isEmpty()) {
            return null;
        }
        return Long.valueOf(id);
    }

    VocCategoryDTO vocCategoryEntity2DTO(VocCategoryDO vocCategoryDO);

    List<VocCategoryDTO> vocCategoryEntities2DTOs(List<VocCategoryDO> vocCategoryDOs);

    @Mapping(source = "vocCategoryDOs", target = "pageList")
    @Mapping(source = "total", target = "totalCount")
    PageDTO<VocCategoryDTO> convertVocCategoryListToPageDTO(List<VocCategoryDO> vocCategoryDOs, Long total);

    DeleteBookCategoryOperateDTO vocCategoryDOToDeleteCategoryDTO(VocCategoryDO vocCategoryDO);

    List<DeleteBookCategoryOperateDTO> vocCategoryDOsToDeleteCategoryDTOs(List<VocCategoryDO> vocCategoryDOs);
}
